//
// Created by kali on 1/25/22.
//

#include "CMServiceBase.h"

CMServiceBase::CMServiceBase(const char *name) {
    m_name = name;
}

CMServiceBase::~CMServiceBase() {

}

int CMServiceBase::registerRecv(int type, cm_service_base_callback *cb) {
    m_lock.lock(__func__ , __LINE__);
    if (m_cb_list.find(type) != m_cb_list.end()) {
        // 没有找到
        std::list<cm_service_base_callback *> item;
        item.push_back(cb);
        m_cb_list[type] = item;
    }
    else
    {
        m_cb_list[type].push_back(cb);
    }
    size_t size = m_cb_list.size();
    m_lock.unlock();
    return size;
}

int CMServiceBase::registerRecv(int type, CMServiceBaseFunction *func) {
    m_lock.lock(__func__ ,__LINE__);
    if (m_func_list.find(type) != m_func_list.end()) {
        // 没有找到
        std::list<CMServiceBaseFunction *> item;
        item.push_back(func);
        m_func_list[type] = item;
    }
    else
    {
        m_func_list[type].push_back(func);
    }
    size_t size = m_func_list.size();
    m_lock.unlock();
    return size;
}

void CMServiceBase::clear() {
    m_lock.lock(__func__ , __LINE__);
    m_cb_list.clear();
    m_func_list.clear();
    m_lock.unlock();
}
